package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.Document;
import com.kaishengit.entity.Employee;
import com.kaishengit.entity.Goal;
import com.kaishengit.entity.Idea;
import com.kaishengit.entity.IdeaComment;
import com.kaishengit.util.db.DBHelp;
import com.kaishengit.util.db.Page;
import com.kaishengit.util.db.RowMapper;

public class IdeaDao {
	private DBHelp<Idea> db = new DBHelp<Idea>();

	public void saveIdea(Idea idea) {
		String sql = "insert into t_idea (id,title,content,employeeid,projectid) values (?,?,?,?,?)";
		db.executeSQL(sql, idea.getId(),idea.getTitle(),idea.getContent(),idea.getEmployeeid(),idea.getProjectid());
		
	}
	public int count(String pid) {
		String sql = "SELECT COUNT(*) FROM t_idea WHERE projectid = ? ";
		return db.executeQueryToNumber(sql,pid).intValue();
	}

	public Page<Idea> findByPid(int pageNum,String pid) {
		Page<Idea> page = new Page<Idea>(count(pid), pageNum);
		String sql = "select t_idea.id,title,content,t_idea.createtime,employeeid,projectid,username from t_idea ,t_employee where t_idea.employeeid = t_employee.id  and  projectid = ? limit ?,?";
		List<Idea> list =  db.executeQueryToList(sql, new RowMapper<Idea>(){
			public Idea mapRow(ResultSet rs) throws SQLException {
				Idea idea = new Idea();
				idea.setId(rs.getString("id"));
				idea.setTitle(rs.getString("title"));
				idea.setContent(rs.getString("content"));
				idea.setEmployeeid(rs.getString("employeeid"));
				idea.setProjectid(rs.getString("projectid"));
				idea.setCreatetime(rs.getString("createtime"));
				Employee employee = new Employee();
				employee.setId(rs.getString("employeeid"));
				employee.setUsername(rs.getString("username"));
				
				idea.setEmployee(employee);
				return idea;
			}
			
		}, pid,page.getStart(),page.getPageSize());
		page.setList(list);
		return page;
	}
	public com.kaishengit.entity.Idea findById(String id) {
		String sql = "select id ,title,content,createtime,employeeid,projectid from t_idea where id = ?";
		return db.executeQueryToObject(sql, new IdeaRowMapper(), id);
	}
	public class IdeaRowMapper implements RowMapper<Idea>{

		public Idea mapRow(ResultSet rs) throws SQLException {
			Idea idea = new Idea();
			idea.setId(rs.getString("id"));
			idea.setTitle(rs.getString("title"));
			idea.setContent(rs.getString("content"));
			idea.setCreatetime(rs.getString("createtime"));
			idea.setEmployeeid(rs.getString("employeeid"));
			idea.setProjectid(rs.getString("projectid"));
			return idea;
		}
		
	}
	public void editIdea(String id, String title, String content) {
		String sql = "update t_idea set title=?,content=? where id = ?";
		db.executeSQL(sql, title,content,id);
	}
	public void delIdea(String id) {
		String sql = "delete from t_idea where id = ?";
		db.executeSQL(sql, id);
		
	}
	public void saveIdeaComment(IdeaComment ic) {
		String sql = "insert into t_ideacomment (id,content,ideaid,employeeid) values(?,?,?,?)";
		db.executeSQL(sql, ic.getId(),ic.getContent(),ic.getIdeaid(),ic.getEmployeeid());
		
	}
	
}
